<?php

namespace App\Controller\App;

use App\Base\BaseController4Restful;
use App\Entity\Task;
use App\Entity\Usercontact;
use App\Repository\TaskRepository;
use App\Repository\UsercontactRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @route("/app")
 */
class ShopTaskController extends BaseController4Restful
{
    /**
     * @Route("/shop/task/match", name="AppShopTaskMatch",methods={"GET","POST"})
     */
    public function AppShopTaskMatch(TaskRepository $repository)
    {
        $user = $this->getUser();
        if (!$user) {return $this->json_out(200,"获取成功",[]);}
        if (!$user->getShop()) {return $this->json_out(200,"请先完成商户入住",[]);}
        $qb = $repository->createQueryBuilder("A");
        $qb->select("A as task");
        $qb->where("A.type = 1");
        //todo 这里需要排除一下个人自己发布的订单
        $qb->orderBy("A.createAt","DESC");

        //人物属性匹配机制
        $qb->innerJoin("App:Shop","Shop");
        $qb->innerJoin("Shop.categories","Cat");
        $qb->andWhere("Cat = A.cat2");
        //地理位置排序
        if (!$lat = $this->getParamRequired("lat")) {
            $lat = 0;
        }
        if (!$lon = $this->getParamRequired("lon")) {
            $lon = 0;
        }
        if ($lat*$lon) {
            $qb->leftJoin("A.address","B");
            $qb->addSelect("SQRT(($lat - B.lat)*($lat - B.lat) + ($lon  - B.lon)*($lon  - B.lon)) AS distance");
//            $qb->orderBy("SQRT(($lat - B.lat)*($lat - B.lat) + ($lon  - B.lon)*($lon  - B.lon))","ASC");
            $qb->orderBy("distance","ASC");
        }
        $tasks = $qb->getQuery()->execute();

        $this->setGroups("task");
        return $this->json_out(200,"获取成功",$tasks);
    }
    /**
     * @Route("/shop/task/vote", name="AppShopTaskVote",methods={"GET","POST"})
     */
    public function AppShopTaskVote(TaskRepository $repository)
    {
        $user = $this->needToBeUser();
        $qb = $repository->createQueryBuilder("A");
        $qb->join("App:TaskVote","B","with","A = B.task AND B.user = ".$user->getId());
        $qb->select("A as task");
        $qb->where("A.status >= 0");
        $qb->andWhere("A.status < 300");
        $qb->orderBy("A.id","DESC");
        $data = $qb->getQuery()->execute();
        $this->setGroups("task");

        return $this->json_out(200,"获取成功",$data);
    }
    /**
     * @Route("/shop/task/hire", name="AppShopTaskHire",methods={"GET","POST"})
     */
    public function AppShopTaskHire(TaskRepository $repository)
    {
        $user = $this->needToBeUser();
        $qb = $repository->createQueryBuilder("A");
        $qb->join("App:TaskVote","B","with","A = B.task AND B.user = ".$user->getId());
        $qb->select("A as task");
        $qb->where("A.status >= 300");
        $qb->andWhere("A.status <= 600");
//        $qb->addSelect("B.status,B.price");//暂时先不加入
        $qb->orderBy("A.id","DESC");
        $data = $qb->getQuery()->execute();

        $this->setGroups("task");

        return $this->json_out(200,"获取成功",$data);
    }
    /**
     * @Route("/shop/task/done", name="AppShopTaskDone",methods={"GET","POST"})
     */
    public function AppShopTaskDone(TaskRepository $repository)
    {
        $user = $this->needToBeUser();
        $qb = $repository->createQueryBuilder("A");
        $qb->join("App:TaskVote","B","with","A = B.task AND B.user = ".$user->getId());
        $qb->select("A as task");
        $qb->where("A.status >= 900");
//        $qb->addSelect("B.status,B.price");//暂时先不加入
        $qb->orderBy("A.id","DESC");
        $data = $qb->getQuery()->execute();

        $this->setGroups("task");

        return $this->json_out(200,"获取成功",$data);
    }
}
